package demoapp.spring.interception;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import org.springframework.beans.factory.FactoryBean;

public class HelloWorldFactoryBean implements FactoryBean<HelloWorld> {

	static class HelloWorldProxy implements MethodInterceptor {
		static HelloWorld newInstance() {
			Enhancer e = new Enhancer();
			e.setSuperclass(HelloWorld.class);
			e.setCallback(new HelloWorldProxy());
			Object bean = e.create();
			return (HelloWorld)bean;
		}

		public Object intercept(Object obj, Method method, Object[] args,
				MethodProxy proxy) throws Throwable {
			return proxy.invokeSuper(obj, args);
		}

	}

	public HelloWorld getObject() throws Exception {
		return HelloWorldProxy.newInstance();
	}

	public Class<?> getObjectType() {
		return HelloWorld.class;
	}

	public boolean isSingleton() {
		return false;
	}

}
